package logic;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.supercsv.io.CsvMapReader;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.io.ICsvMapReader;
import org.supercsv.io.ICsvMapWriter;
import org.supercsv.prefs.CsvPreference;

import com.github.mhendred.face4j.DefaultFaceClient;
import com.github.mhendred.face4j.FaceClient;
import com.github.mhendred.face4j.exception.FaceAPIException;
import com.github.mhendred.face4j.exception.FaceClientException;
import com.github.mhendred.face4j.exception.FaceServerException;
import com.github.mhendred.face4j.model.Face;
import com.github.mhendred.face4j.model.Photo;

public class LogicFace {
	private String API_KEY = "f1454d2b088878a684ee6ed43ceb3055";

	private String API_SEC = "d2f88bed9cd6485ae94ad5791885d62c";
	private final double TOTAL_PORC = 100.0;
	private final float WIDTH = 50;
	private final float HEIGHT = 60;
	private FaceClient faceClient = null;

	private final String GENDER_MASC = "GenderMale";
	private final String WEAR_GLASSES = "WearGlasses";
	private final String SMILING = "Smiling";
	private final String DISTANCE_EYES = "Dist Eyes";
	private final String DISTANCE_EYE_MOUTH_LEFT = "Dist Eye Mouth Left";
	private final String DISTANCE_EYE_MOUTH_RIGHT = "Dist Eye Mouth Right";
	private final String DISTANCE_NOSE_EYE_LEFT = "Dist Nose Eye Left";
	private final String DISTANCE_NOSE_EYE_RIGHT = "Dist Nose Eye Right";
	private final String DISTANCE_NOSE_MOUTH = "Dist Nose Mouth Center";
	private String pathCSV;
	private String pathImages;

	private double pesoGender = 0.4;
	private double pesoSmiling = 0.4;
	private double pesoGlasses = 0.2;
	private double pesoDistanceEyes = 0.2;
	private double pesoDistanceEyeMouthLeft = 0.16;
	private double pesoDistanceEyeMouthRight = 0.16;
	private double pesoDistanceNoseEyeLeft= 0.16;
	private double pesoDistanceNoseEyeRight= 0.16;
	private double pesoDistanceNoseMouth= 0.16;
	private double pesoFisico = 0.5;
	private double pesoEstado = 0.5;
	
	private double alfa = 0.2;
	private double alfa2 = 0.1;
	
	private static LogicFace instance;
	
	public static LogicFace getInstance(String apiKey, String apiSec) {
		if(instance == null){
			instance = new LogicFace(apiKey, apiSec);
		}
		return instance;
	}
	
	public static void main(String[] args) throws FaceClientException,
			FaceServerException {
		LogicFace logicFace = new LogicFace("f1454d2b088878a684ee6ed43ceb3055",
				"d2f88bed9cd6485ae94ad5791885d62c");
		
		logicFace.setPathCSV("C:\\Users\\Delano\\Pictures\\DBImages.csv");
		//logicFace.setPathImages("D:\\UFCG\\VC\\projeto\\image-vc");
		//logicFace.generateSpreadsheet();
		
		
	}

	private LogicFace(String api_key, String api_sec) {
		API_KEY = api_key;
		API_SEC = api_sec;
		this.faceClient = new DefaultFaceClient(API_KEY, API_SEC);
	}
	
	
	
	public double getPesoFisico() {
		return pesoFisico;
	}

	public void setPesoFisico(double pesoFisico) {
		this.pesoFisico = pesoFisico;
	}

	public double getPesoEstado() {
		return pesoEstado;
	}

	public void setPesoEstado(double pesoEstado) {
		this.pesoEstado = pesoEstado;
	}

	public double getPesoGender() {
		return pesoGender;
	}

	public void setPesoGender(double pesoGender) {
		this.pesoGender = pesoGender;
	}

	public double getPesoSmiling() {
		return pesoSmiling;
	}

	public void setPesoSmiling(double pesoSmiling) {
		this.pesoSmiling = pesoSmiling;
	}

	public double getPesoGlasses() {
		return pesoGlasses;
	}

	public void setPesoGlasses(double pesoGlasses) {
		this.pesoGlasses = pesoGlasses;
	}

	public void setPathCSV(String pathCSV) {
		this.pathCSV = pathCSV;
	}
	
	public void setPathImages(String pathImages) {
		this.pathImages = pathImages;
	}
	
	public String getPathCSV() {
		return pathCSV;
	}
	
	public String getPathImages() {
		return pathImages;
	}
	
	public File getImage(String name){
		File path = new File(pathImages);
		for(File f : path.listFiles()){
			if(f.getName().contains(name)){
				return f;
			}
		}
		return null;
	}

	public Face getFace(File file) throws FaceClientException,
			FaceServerException {
		Photo photo = this.faceClient.detect(file);
		return photo.getFace();
	}

	public TreeMap<String, TreeMap<String, String>> extractDatas(String path)
			throws FaceClientException, FaceServerException {
		TreeMap<String, TreeMap<String, String>> datas = new TreeMap<String, TreeMap<String, String>>();

		File file = new File(path);
		File[] images = file.listFiles();
		for (int i = 0; i < images.length; i++) {
			if (images[i].getAbsolutePath().endsWith(".jpg")
					|| images[i].getAbsolutePath().endsWith(".png")) {
			datas.put(clearImageName(images[i].getName()), extractData(images[i]));
			}
		}
		return datas;
	}
	
	private TreeMap<String, String> extractNameAndDescription(String name) {
		
		TreeMap<String, String> datas = new TreeMap<String, String>();
		
		String name2 = "Character";
		String description = "No Description";
		String site = "";
		
		int position = name.indexOf("_");
		name2 = name.substring(0, position).replace('-', ' ');
		
		if(name.substring(position+1, name.length()).equalsIgnoreCase("resident-evil")) {
			description = "This character is the game Resident Evil. More information in this site: http://www.residentevil.com/";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("mass-effect")){
			description = "This character is the game Mass Effect. More information in this site: http://masseffect.bioware.com/";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("uncharted3")){
			description = "This character is the game Uncharted 3. More information in this site: http://www.uncharted3.org/";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("fable-iii")){
			description = "This character is the game Fable 3. More information in this site: http://lionhead.com/Fable/FableIII/";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("shrek")){
			description = "This character is the game Shrek. More information in this site: http://shrek.com/";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("final-fantasy")){
			description = "This character is the game Final Fantasy. More information in this site: http://www.finalfantasy.com.br/";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("red-dead-redemption")){
			description = "This character is the game Red Dead Redemption. More information in this site: http://www.rockstargames.com/reddeadredemption/";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("tomb-raider")){
			description = "This character is the game Tomb Raider. More information in this site: http://www.tombraider.com/";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("max-payne")){
			description = "This character is the game Max Payne. More information in this site: http://www.rockstargames.com/maxpayne/agegate.html";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("pes2012")){
			description = "This character is the game PES 2012. More information in this site: http://www.konami.com/games/pes2012";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("metal-gear-solid4")){
			description = "This character is the game Metal Gear Solid 4. More information in this site: http://www.konami.jp/mgs4/global/index.html";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("tom-clancy's-splinter-cell")){
			description = "This character is the game Tom Clancy's Splienter Cell. More information in this site: http://splintercell.us.ubi.com/conviction/";
		}else if(name.substring(position+1, name.length()).equalsIgnoreCase("mortal-kombat-9")){
			description = "This character is the game Mortal Kombat 9. More information in this site: http://www.themortalkombat.com/";
		}else{
			description = "This character is the game "+name.substring(position+1, name.length());
		}
		
		datas.put(name,description);
		
		return datas;
	}
	
	private TreeMap<String, String> extractData(File image) throws FaceClientException, FaceServerException {
		
			Face face = getFace(image);
			TreeMap<String, String> imageDatas = new TreeMap<String, String>();

			if (face != null) {
				imageDatas.put(this.GENDER_MASC, getGenderConfidence(face));
				imageDatas.put(this.WEAR_GLASSES,
						getGlassesConfidence(face));
				imageDatas.put(this.SMILING, getSmilingConfidence(face));

				double leftEyeY = normalizePoint(face.getLeftEye().y,
						this.HEIGHT, face.getHeight());
				double leftEyeX = normalizePoint(face.getLeftEye().x,
						this.WIDTH, face.getWidth());
				double rightEyeY = normalizePoint(face.getRightEye().y,
						this.HEIGHT, face.getHeight());
				double rightEyeX = normalizePoint(face.getRightEye().x,
						this.WIDTH, face.getWidth());
				double leftMouthY = normalizePoint(face.getMouthLeft().y,
						this.HEIGHT, face.getHeight());
				double leftMouthX = normalizePoint(face.getMouthLeft().x,
						this.WIDTH, face.getWidth());
				double rightMouthY = normalizePoint(face.getMouthRight().y,
						this.HEIGHT, face.getHeight());
				double rightMouthX = normalizePoint(face.getMouthRight().x,
						this.WIDTH, face.getWidth());
				double centerMouthY = normalizePoint(
						face.getMouthCenter().y, this.HEIGHT,
						face.getHeight());
				double centerMouthX = normalizePoint(
						face.getMouthCenter().x, this.WIDTH,
						face.getWidth());
				double noseY = normalizePoint(face.getNose().y,
						this.HEIGHT, face.getHeight());
				double noseX = normalizePoint(face.getNose().x, this.WIDTH,
						face.getWidth());

				imageDatas.put(
						this.DISTANCE_EYES,
						fixNumber(getTwoPointsDistance(rightEyeX, rightEyeY,
								leftEyeX, leftEyeY) + ""));
				imageDatas.put(
						this.DISTANCE_EYE_MOUTH_LEFT,
						fixNumber(getTwoPointsDistance(leftEyeX, leftEyeY,
								leftMouthX, leftMouthY) + ""));

				imageDatas.put(
						this.DISTANCE_EYE_MOUTH_RIGHT,
						fixNumber(getTwoPointsDistance(rightEyeX, rightEyeY,
								rightMouthX, rightMouthY) + ""));

				imageDatas.put(
						this.DISTANCE_NOSE_EYE_LEFT,
						fixNumber(getTwoPointsDistance(leftEyeX, leftEyeY, noseX,
								noseY) + ""));

				imageDatas.put(
						this.DISTANCE_NOSE_EYE_RIGHT,
						fixNumber(getTwoPointsDistance(rightEyeX, rightEyeY, noseX,
								noseY) + ""));

				imageDatas.put(
						this.DISTANCE_NOSE_MOUTH,
						fixNumber(getTwoPointsDistance(centerMouthX, centerMouthY,
								noseX, noseY) + ""));
		}
		return imageDatas;
	}

	private String getGenderConfidence(Face face) {
		if (face.getGender().equals("female")) {
			return (this.TOTAL_PORC - face.getGenderConfidence()) + "";
		} else {
			return face.getGenderConfidence() + "";
		}
	}

	private String getGlassesConfidence(Face face) {
		if (face.isWearingGlasses()) {
			return face.getGlassesConfidence() + "";
		} else {
			return (this.TOTAL_PORC - face.getGlassesConfidence()) + "";
		}
	}

	private String getSmilingConfidence(Face face) {
		if (face.isSmiling()) {
			return face.getSmilingConfidence() + "";
		} else {
			return (this.TOTAL_PORC - face.getSmilingConfidence()) + "";
		}
	}

	private String clearImageName(String name) {
		return name.replace(".jpg", "").replace(".JPG", "").replace(".png", "")
				.replace(".PNG", "");
	}

	public void generateSpreadsheet()
			throws IOException, FaceClientException, FaceServerException {

		TreeMap<String, TreeMap<String, String>> imageDatas = extractDatas(pathImages);
		File file = new File(pathCSV);
		file.createNewFile();
		FileWriter fileWriter = new FileWriter(file.getAbsolutePath());
		ICsvMapWriter writer = new CsvMapWriter(fileWriter,
				CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);

		String[] header = new String[] { "Cartoon", GENDER_MASC, WEAR_GLASSES,
				SMILING, DISTANCE_EYES, DISTANCE_EYE_MOUTH_LEFT,
				DISTANCE_EYE_MOUTH_RIGHT, DISTANCE_NOSE_EYE_LEFT,
				DISTANCE_NOSE_EYE_RIGHT, DISTANCE_NOSE_MOUTH };

		writer.writeHeader(header);

		for (String att : imageDatas.keySet()) {
			HashMap<String, ? super Object> data = new HashMap<String, Object>();
			data.put(header[0], att);
			TreeMap<String, String> auxAtts = imageDatas.get(att);
			for (int i = 1; i < header.length; i++) {

				data.put(header[i],
						fixNumber(auxAtts.get(header[i]).toString()));

			}
			writer.write(data, header);
		}

		writer.close();
	}

	private double getTwoPointsDistance(double rightX, double rightY,
			double leftX, double leftY) {
		return Math.sqrt(Math.pow(rightX - leftX, 2)
				+ Math.pow(rightY - leftY, 2));
	}

	private float getPorcentage(float value, float normalizeValue) {
		return (normalizeValue / value);
	}

	private double normalizePoint(double point, double normalizeValue,
			double value) {
		return point * (normalizeValue / value);
	}

	private String fixNumber(String number) {
		if (number.length() > 4) {
			return number.substring(0, 4);
		}
		return number;
	}
	
	private Double compareFaces(TreeMap<String, String> face1, TreeMap<String, String> face2) {
		
		//etapa1 - fisicas
		
		ArrayList<Double> dadosFisicos1 = new ArrayList<Double>();
		ArrayList<Double> dadosFisicos2 = new ArrayList<Double>();
		
		dadosFisicos1.add(this.TOTAL_PORC);
		dadosFisicos1.add(this.TOTAL_PORC);
		dadosFisicos1.add(this.TOTAL_PORC);
		dadosFisicos1.add(this.TOTAL_PORC);
		dadosFisicos1.add(this.TOTAL_PORC);
		dadosFisicos1.add(this.TOTAL_PORC);
		
		dadosFisicos2.add(Double.parseDouble(face2.get(this.DISTANCE_EYE_MOUTH_LEFT)) / Double.parseDouble(face1.get(this.DISTANCE_EYE_MOUTH_LEFT)) * this.TOTAL_PORC);
		dadosFisicos2.add(Double.parseDouble(face2.get(this.DISTANCE_EYE_MOUTH_RIGHT)) / Double.parseDouble(face1.get(this.DISTANCE_EYE_MOUTH_RIGHT)) * this.TOTAL_PORC);
		dadosFisicos2.add(Double.parseDouble(face2.get(this.DISTANCE_EYES)) / Double.parseDouble(face1.get(this.DISTANCE_EYES)) * this.TOTAL_PORC);
		dadosFisicos2.add(Double.parseDouble(face2.get(this.DISTANCE_NOSE_EYE_LEFT)) / Double.parseDouble(face1.get(this.DISTANCE_NOSE_EYE_LEFT)) * this.TOTAL_PORC);
		dadosFisicos2.add(Double.parseDouble(face2.get(this.DISTANCE_NOSE_EYE_RIGHT)) / Double.parseDouble(face1.get(this.DISTANCE_NOSE_EYE_RIGHT)) * this.TOTAL_PORC);
		dadosFisicos2.add(Double.parseDouble(face2.get(this.DISTANCE_NOSE_MOUTH)) / Double.parseDouble(face1.get(this.DISTANCE_NOSE_MOUTH)) * this.TOTAL_PORC);
		
		double simFisico = similarity(dadosFisicos1, dadosFisicos2, true);
		
		//etapa2 - estado
		
		ArrayList<Double> dadosEstado1 = new ArrayList<Double>();
		ArrayList<Double> dadosEstado2 = new ArrayList<Double>();
		
		dadosEstado1.add(Double.parseDouble(face1.get(this.GENDER_MASC)) * pesoGender);
		dadosEstado1.add(Double.parseDouble(face1.get(this.SMILING)) * pesoSmiling);
		dadosEstado1.add(Double.parseDouble(face1.get(this.WEAR_GLASSES)) * pesoGlasses);
		
		dadosEstado2.add(Double.parseDouble(face2.get(this.GENDER_MASC)) * pesoGender);
		dadosEstado2.add(Double.parseDouble(face2.get(this.SMILING)) * pesoSmiling);
		dadosEstado2.add(Double.parseDouble(face2.get(this.WEAR_GLASSES)) * pesoGlasses);
		
		double simEstado = similarity(dadosEstado1, dadosEstado2, false);
		
		return (simFisico * pesoFisico) + (simEstado * pesoEstado);
	}
	
	private double similarity(ArrayList<Double> dados1, ArrayList<Double> dados2, boolean media){
		if(dados1.size() != dados2.size()){
			return 0;
		}
		
		double somaDasDiferencas = 0;
		
		for(int i = 0; i < dados1.size(); i++){
			somaDasDiferencas += Math.abs(dados1.get(i) - dados2.get(i));
		}
		
		double mean = somaDasDiferencas;
		
		if(media){
			mean = somaDasDiferencas/dados1.size();
		}
		
		return 100 - mean;
	}
	
	private TreeMap<String, TreeMap<String, String>> getFaces() throws IOException {
		
		ICsvMapReader reader = new CsvMapReader(new FileReader(pathCSV), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
		
		TreeMap<String, TreeMap<String, String>> map = new TreeMap<String, TreeMap<String,String>>();
		
		Map<String, String> data;
		String[] header = reader.getCSVHeader(true);
		while((data = reader.read(header)) != null){
			String name = data.get("Cartoon");
			data.remove("Cartoon");
			map.put(name, new TreeMap<String, String>(data));
		}
		
		return map;
	}
	
	public Score searchFace(File nameCartoon) throws FaceClientException, FaceServerException, IOException{
		
		Score score = new Score();
		
		
		TreeMap<String, TreeMap<String, String>> facesDB = getFaces();
		TreeMap<String, String> faceUser = extractData(nameCartoon);
		
		for(String name : facesDB.keySet()){
			
			ScoreElement element = new ScoreElement(name);
			element.value = compareFaces(faceUser, facesDB.get(name));
			score.addElement(element);
			
		}
		
		return score;
		
	}
	
	private double diference(String value1, String value2){
		return Math.abs(Double.parseDouble(value1) - Double.parseDouble(value2));
	}
	
	private void adjustEstado(Score score){
		
		ScoreElement last = score.last();
		
		if(last.name.equals(GENDER_MASC) && pesoGender < 1){
			
			pesoGender += alfa;
			pesoGlasses -= alfa/2;
			pesoSmiling -= alfa/2;
			
		}
		
		else if(last.name.equals(SMILING) && pesoSmiling < 1){
			
			pesoGender -= alfa/2;
			pesoGlasses -= alfa/2;
			pesoSmiling += alfa;
			
		}
		
		else if(last.name.equals(WEAR_GLASSES) && pesoGlasses < 1){
			
			pesoGender -= alfa/2;
			pesoGlasses += alfa;
			pesoSmiling -= alfa/2;
			
		}
		
	}
	
	private void adjustFisico(Score score){
		
		ScoreElement last = score.last();
		
		if(last.name.equals(DISTANCE_EYE_MOUTH_LEFT) && pesoDistanceEyeMouthLeft < 1){
			
			pesoDistanceEyeMouthLeft += alfa2;
			pesoDistanceEyeMouthRight -= alfa2/5;
			pesoDistanceEyes -= alfa2/5;
			pesoDistanceNoseEyeLeft -= alfa2/5;
			pesoDistanceNoseEyeRight -= alfa2/5;
			pesoDistanceNoseMouth -= alfa2/5;
			
		}
		
		else if(last.name.equals(DISTANCE_EYE_MOUTH_RIGHT) && pesoDistanceEyeMouthRight < 1){
			
			pesoDistanceEyeMouthLeft -= alfa2/5;
			pesoDistanceEyeMouthRight += alfa2;
			pesoDistanceEyes -= alfa2/5;
			pesoDistanceNoseEyeLeft -= alfa2/5;
			pesoDistanceNoseEyeRight -= alfa2/5;
			pesoDistanceNoseMouth -= alfa2/5;
			
		}
		
		else if(last.name.equals(DISTANCE_EYES) && pesoDistanceEyes < 1){
			
			pesoDistanceEyeMouthLeft -= alfa2/5;
			pesoDistanceEyeMouthRight -= alfa2/5;
			pesoDistanceEyes += alfa2;
			pesoDistanceNoseEyeLeft -= alfa2/5;
			pesoDistanceNoseEyeRight -= alfa2/5;
			pesoDistanceNoseMouth -= alfa2/5;
			
		}
		
		else if(last.name.equals(DISTANCE_NOSE_EYE_LEFT) && pesoDistanceNoseEyeLeft < 1){
			
			pesoDistanceEyeMouthLeft -= alfa2/5;
			pesoDistanceEyeMouthRight -= alfa2/5;
			pesoDistanceEyes -= alfa2/5;
			pesoDistanceNoseEyeLeft += alfa2;
			pesoDistanceNoseEyeRight -= alfa2/5;
			pesoDistanceNoseMouth -= alfa2/5;
			
		}
		
		else if(last.name.equals(DISTANCE_NOSE_EYE_RIGHT) && pesoDistanceNoseEyeRight < 1){
			
			pesoDistanceEyeMouthLeft -= alfa2/5;
			pesoDistanceEyeMouthRight -= alfa2/5;
			pesoDistanceEyes -= alfa2/5;
			pesoDistanceNoseEyeLeft -= alfa2/5;
			pesoDistanceNoseEyeRight += alfa2;
			pesoDistanceNoseMouth -= alfa2/5;
			
		}
		
		else if(last.name.equals(DISTANCE_NOSE_MOUTH) && pesoDistanceNoseMouth < 1){
			
			pesoDistanceEyeMouthLeft -= alfa2/5;
			pesoDistanceEyeMouthRight -= alfa2/5;
			pesoDistanceEyes -= alfa2/5;
			pesoDistanceNoseEyeLeft -= alfa2/5;
			pesoDistanceNoseEyeRight -= alfa2/5;
			pesoDistanceNoseMouth += alfa2;
			
		}
		
	}
	
	public boolean adjust(File user, String nameCartoon) throws FaceClientException, FaceAPIException, IOException{
		
		try{
			TreeMap<String, TreeMap<String, String>> faces = getFaces();
			TreeMap<String, String> dataUser = extractData(user);
			TreeMap<String, String> dataCartoon = faces.get(nameCartoon);
			
			Score scoreEstado = new Score();
			
			
			scoreEstado.addElement(new ScoreElement(GENDER_MASC, diference(dataUser.get(GENDER_MASC), dataCartoon.get(GENDER_MASC))));
			scoreEstado.addElement(new ScoreElement(SMILING, diference(dataUser.get(SMILING), dataCartoon.get(SMILING))));
			scoreEstado.addElement(new ScoreElement(WEAR_GLASSES, diference(dataUser.get(WEAR_GLASSES), dataCartoon.get(WEAR_GLASSES))));
			
			adjustEstado(scoreEstado);
			
			Score scoreFisico = new Score();
			
			scoreFisico.addElement(new ScoreElement(DISTANCE_EYE_MOUTH_LEFT, diference(dataUser.get(DISTANCE_EYE_MOUTH_LEFT), dataCartoon.get(DISTANCE_EYE_MOUTH_LEFT))));
			scoreFisico.addElement(new ScoreElement(DISTANCE_EYE_MOUTH_RIGHT, diference(dataUser.get(DISTANCE_EYE_MOUTH_RIGHT), dataCartoon.get(DISTANCE_EYE_MOUTH_RIGHT))));
			scoreFisico.addElement(new ScoreElement(DISTANCE_EYES, diference(dataUser.get(DISTANCE_EYES), dataCartoon.get(DISTANCE_EYES))));
			scoreFisico.addElement(new ScoreElement(DISTANCE_NOSE_EYE_LEFT, diference(dataUser.get(DISTANCE_NOSE_EYE_LEFT), dataCartoon.get(DISTANCE_NOSE_EYE_LEFT))));
			scoreFisico.addElement(new ScoreElement(DISTANCE_NOSE_EYE_RIGHT, diference(dataUser.get(DISTANCE_NOSE_EYE_RIGHT), dataCartoon.get(DISTANCE_NOSE_EYE_RIGHT))));
			scoreFisico.addElement(new ScoreElement(DISTANCE_NOSE_MOUTH, diference(dataUser.get(DISTANCE_NOSE_MOUTH), dataCartoon.get(DISTANCE_NOSE_MOUTH))));
			
			adjustFisico(scoreFisico);
			
			/*
			if(meanEstado < meanFisico){
				pesoEstado += alfa2;
				pesoFisico -= alfa2;
			}
			
			else if(meanEstado > meanFisico){
				pesoEstado -= alfa3;
				pesoFisico += alfa3;
			}		
			*/
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
}